// pages/print/print.js


const { CPCL } = require('../../utils/cpcl_print/bluetooth-v2/index')
const { SF } = require('../../utils/cpcl_print/template/shunfeng')
const { Custom } = require('../../utils/cpcl_print/template/custom')

const ecUI = require("../../utils/ecUI.js");
const ecBLE = require("../../utils/ecBLE.js");


let sendData
let ctx
Page({

    /**
     * 页面的初始数据
     */
    data: {
        type:'E26T209',
        model: 'HQA2D-27H320T-HX3 HQA2D-27H320T-HX4 HQA2D-27H320T-HX5 HQA2D-27H320T-HX4 HQA2D-27H320T-HX5',
        displacement:'26',
        refrigerant:'POE68',
        voltage: '195-450',
        rpm: '1500-3000',
        po: 'S02505230001',
        company: '泉州生辉'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        ctx = this
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        ctx = this
        ctx = this;
        sendData = "";
        //on disconnect
        ecBLE.onBLEConnectionStateChange(ctx.bleStateChange);
        //receive data
        ecBLE.onBLECharacteristicValueChange(ctx.bleValueChange);
    },
    printHandler() {
        console.log('打印')
        const command = Custom({
          type: ctx.data.type,
          model: ctx.data.model,
          displacement: ctx.data.displacement,
          refrigerant: ctx.data.refrigerant,
          voltage: ctx.data.voltage,
          rpm: ctx.data.rpm,
          po: ctx.data.po,
          company: ctx.data.company,
          qrCode: "https://www.hao123.com",
        })
        ctx.printCommand(command.getData())
    },

      // 打印命令
    printCommand: function (data) {
      var uint8Buf = Array.from(data);
      let sendData64 = [];
      function splitArray(data, size) {
        var result = {};
        var j = 0
        for (var i = 0; i < data.length; i += size) {
          result[j] = data.slice(i, i + size)
          let resultMap = [...result[j]]

          let buffer = new ArrayBuffer(result[j].length)
          let dataView = new DataView(buffer)
          for (let k = 0; k < resultMap.length; k++) {
            dataView.setUint8(k, resultMap[k]);
          }
          sendData64[j] = buffer
          j++
        }
        return result
      }
      splitArray(uint8Buf, 20)
      sendData = sendData64
      ecBLE.writeBLECharacteristicValuePrint(sendData64, 1);
    },

     bleStateChange() {
    ecUI.showModal("提示", "设备断开连接", () => {
      log("reLaunch");
      wx.reLaunch({
        url: "/pages/index/index",
      });
    });
  },
  bleValueChange(str, strHex) {
    console.log("bleValueChange", strHex);
  },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onUnload() {
      ecBLE.onBLEConnectionStateChange(() => {});
      ecBLE.onBLECharacteristicValueChange(() => {});
      ecBLE.closeBLEConnection();
    },
    onHide() {
      ecBLE.onBLEConnectionStateChange(() => {});
      ecBLE.onBLECharacteristicValueChange(() => {});
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})